package com.hz.filesystem.controller;


import com.alibaba.fastjson.JSONObject;
import com.hz.common.utils.ResponseData;
import com.hz.filesystem.entity.HzCityEntity;
import com.hz.filesystem.entity.HzFileEntity;
import com.hz.filesystem.service.HzCityService;
import com.hz.filesystem.service.HzFileService;
import com.hz.filesystem.service.HzTeacherService;
import com.hz.filesystem.web.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("publicresource")
@Api(tags="公共资源管理")
public class PublicController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HzFileService hzFileService;

    @Autowired
    HzCityService hzCityService;

    @Autowired
    HzTeacherService hzTeacherService;

    @ApiOperation("根据条件查找当前目录下的文件")
    @GetMapping("/querypublicfilesbyfid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pcatype", value = "0：省1：市2：区/县", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pcaid", value = "省市区/县id", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "ordertype", value = "排序方式0：中文排序", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<List<HzFileEntity>> querypublicfilesbyfid(@RequestParam(value = "pcatype",required = false) Integer pcatype,
                                                                  @RequestParam(value = "pcaid",required = false) Long pcaid,
                                                                  @RequestParam("upperFid") long upperFid,
                                                                  @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzFileEntity> hzFiles = null;
        if(pcatype != null && pcaid != null){
            hzFiles = hzFileService.querypublicfilesbyfidpca(upperFid, userId, pcatype, pcaid);
        }else   hzFiles = hzFileService.querypublicfilesbyfid(upperFid, userId);
        if (hzFiles == null)    return ResponseData.success("该文件不存在或不是一个目录",false);
        return ResponseData.success(hzFiles);
    }

    @ApiOperation("获取文件下载量排行榜")
    @GetMapping("/querypublicfilesbydowncount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<JSONObject> querypublicfilesbydowncount(@RequestParam("curPage") Integer curPage,
                                                                        @RequestParam("pageSize") Integer pageSize,
                                                                        @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzFileEntity> hzFiles = hzFileService.querypublicfilesbydowncount(curPage,pageSize);
        if (hzFiles == null)    return ResponseData.success("该文件不存在或不是一个目录",false);
        JSONObject json = new JSONObject();
        json.put("record",hzFiles);
        json.put("total",hzFileService.querypublicfilesnum());
        json.put("size",pageSize);
        return ResponseData.success(json);
    }

    @ApiOperation("根据状态获取上传的公共教参")
    @GetMapping("/queryfilesbystate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "verifyState", value = "审核状态0：未审核1：已审核2：审核失败3：全部", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<JSONObject> queryfilesbystate(@RequestParam("curPage") Integer curPage,
                                                              @RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam("verifyState") int verifyState,
                                                              @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        JSONObject json = hzFileService.queryfilesbystate(verifyState, userId, curPage, pageSize);
        return ResponseData.success(json);
    }
}
